package com.jqr.activity.myschool.ui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.jqr.activity.myschool.ui.pages.ArticleDetailPage
import com.jqr.activity.myschool.ui.pages.ArticleListPage
import com.jqr.activity.myschool.ui.pages.PageConstant
import com.jqr.activity.myschool.ui.pages.PageConstant.ARTICLE_DETAIL_PAGE
import com.jqr.activity.myschool.ui.pages.PageConstant.ARTICLE_LIST_PAGE
import com.jqr.activity.myschool.ui.theme.MySchoolTheme
import com.jqr.activity.myschool.viewmodels.MainViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MySchoolTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val navController=rememberNavController()
                    val viewModel: MainViewModel = viewModel()
                    NavHost(navController = navController, startDestination = ARTICLE_LIST_PAGE ){

                        composable(ARTICLE_LIST_PAGE){
                            ArticleListPage(navController,viewModel)
                        }
                        composable(
                            "${PageConstant.ARTICLE_DETAIL_PAGE}/{id}",
                            arguments = listOf(
                                navArgument("id"){
                                    type= NavType.IntType
                                    defaultValue=1
                                }
                            )

                        ){
                            val id=it.arguments?.getInt("id")?:1
                            ArticleDetailPage(navController,id)
                        }
                    }
                }
            }
        }
    }
}

